package com.ruoyi.shop.cms.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shop.cms.service.IShopCmsCategoryService;
import com.ruoyi.shop.common.constant.ShopConstants;
import com.ruoyi.shop.common.core.domain.ShopCategory;
import com.ruoyi.shop.common.core.mapper.ShopCategoryMapper;

@Service
public class ShopCmsCategoryServiceImpl implements IShopCmsCategoryService{

	@Autowired
	private ShopCategoryMapper shopCategoryMapper;

	/**
	 * 查询栏目
	 * 
	 * @param categoryId 栏目主键
	 * @return 栏目
	 */
	@Override
	public ShopCategory selectCategoryById(Long categoryId) {
		return shopCategoryMapper.selectCategoryById(categoryId);
	}

	/**
	 * 查询栏目列表
	 * 
	 * @param shopCategory 栏目
	 * @return 栏目
	 */
	@Override
	public List<ShopCategory> selectCategoryList(ShopCategory category) {
		category.setCategoryType(ShopConstants.SHOP_CATEGORY_TYPE_CMS);// 栏目
		return shopCategoryMapper.selectCategoryList(category);
	}

	/**
	 * 新增栏目
	 * 
	 * @param category 栏目
	 * @return 结果
	 */
	@Override
	public int insertCategory(ShopCategory category) {
		category.setCategoryType(ShopConstants.SHOP_CATEGORY_TYPE_CMS);// 栏目
		return shopCategoryMapper.insertCategory(category);
	}

	/**
	 * 修改栏目
	 * 
	 * @param category 栏目
	 * @return 结果
	 */
	@Override
	public int updateCategory(ShopCategory category) {
		category.setCategoryType(ShopConstants.SHOP_CATEGORY_TYPE_CMS);// 栏目
		return shopCategoryMapper.updateCategory(category);
	}

	/**
	 * 批量栏目
	 * 
	 * @param categoryId 栏目主键
	 * @return 结果
	 */
	@Override
	public int deleteCategoryById(Long categoryId) {
		return shopCategoryMapper.deleteCategoryById(categoryId);
	}

	/**
	 * 根据父ID查询数量
	 * 
	 * @param parentId 栏目父主键
	 * @return
	 */
	@Override
	public int selectCategoryCount(Long parentId) {
		ShopCategory category = new ShopCategory();
		category.setParentId(parentId);
		return shopCategoryMapper.selectCategoryCount(category);
	}

	/**
	 * 查询栏目树（排除下级）
	 * 
	 * @param category
	 * @return
	 */
	@Override
	public List<Ztree> selectCategoryTreeExcludeChild(ShopCategory category) {
		Long excludeId = category.getExcludeId();
		category.setCategoryType(ShopConstants.SHOP_CATEGORY_TYPE_CMS);// 栏目
		List<ShopCategory> categorys = shopCategoryMapper.selectCategoryList(category);
		if (excludeId.intValue() > 0) {
			categorys.removeIf(d -> d.getCategoryId().intValue() == excludeId
					|| ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), excludeId + ""));
		}
		List<Ztree> ztrees = initZtree(categorys);
		return ztrees;
	}

	/**
	 * 对象转栏目树
	 *
	 * @param categoryList 栏目列表
	 * @return 树结构列表
	 */
	public List<Ztree> initZtree(List<ShopCategory> categoryList) {
		List<Ztree> ztrees = new ArrayList<Ztree>();
		for (ShopCategory category : categoryList) {
			if (UserConstants.NORMAL.equals(category.getStatus())) {
				Ztree ztree = new Ztree();
				ztree.setId(category.getCategoryId());
				ztree.setpId(category.getParentId());
				ztree.setName(category.getCategoryName());
				ztree.setTitle(category.getCategoryName());
				ztrees.add(ztree);
			}
		}
		return ztrees;
	}
	
	/***
	 * 查询可用商城栏目
	 * 
	 * @return
	 */
	@Override
	public List<ShopCategory> queryCategorys() {
		ShopCategory param = new ShopCategory();
		param.setStatus(UserConstants.NORMAL);// 状态可用
		param.setCategoryType(ShopConstants.SHOP_CATEGORY_TYPE_CMS);// 文章分类(栏目)
		return shopCategoryMapper.selectCategoryList(param);
	}
	
}
